package edu.mse.camel.client.posterboard.ui.editor.dnd;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.Request;
import org.eclipse.gef.dnd.AbstractTransferDropTargetListener;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.dnd.ObjectTransfer;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;

import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardDiagram;

public class PBWBDiagramDropTargetListener extends AbstractTransferDropTargetListener {

	public PBWBDiagramDropTargetListener(GraphicalViewer viewer) {
		super(viewer, ObjectTransfer.getInstance());
	}

	private IFile iFile;
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean isEnabled(DropTargetEvent event) {
		boolean result = super.isEnabled(event);
		if (result) {
			updateTargetRequest();
			updateTargetEditPart();
			ObjectTransfer transfer = (ObjectTransfer) getTransfer();
			if (transfer != null) {
				List l = (List) transfer.getObject();
				if (l.size() > 0) {
					Object o = l.get(0);
					if (o instanceof WhiteboardDiagram) {
						return true;
					}
				}
			}
		}
		return false;
	}

	@Override
	protected void updateTargetRequest() {
		CreateRequest req = (CreateRequest) getTargetRequest();
		req.setLocation(getDropLocation());
		req.setSize(new Dimension(800, 800));
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Request createTargetRequest() {
		CreateRequest req = new CreateRequest();
		if (iFile != null) {
			req.getExtendedData().put(
					"WHITEBOARD", iFile);
		}
		return req;
	}

}
